package com.example.campusforumapi.service.impl;

import com.example.campusforumapi.dao.ArticleMapper;
import com.example.campusforumapi.entity.Activity;
import com.example.campusforumapi.entity.Article;
import com.example.campusforumapi.entity.Category;
import com.example.campusforumapi.entity.User;
import com.example.campusforumapi.service.ArticleService;
import com.example.campusforumapi.util.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    @Override
    public List<Category> getCategories() {
        return articleMapper.getCategories();
    }

    @Override
    public PageResult getArticlePage(Integer page, Integer pageSize, Integer articleId, String username, String title, Integer categoryId) {
        PageHelper.startPage(page,pageSize);
//        2.执行查询
        List<Article> articleList = articleMapper.list(articleId,username,title,categoryId);
        Page<Article> p = (Page<Article>) articleList;
//        3.封装PageResult对象
        PageResult pageResult = new PageResult(p.getResult(),p.getTotal(),p.getPageSize(),p.getPageNum());
        return pageResult;
    }

    @Override
    public int delArticle(List<Integer> ids) {
        return articleMapper.delArticle(ids);
    }

    @Override
    public int insertArticle(Article article) {
        if(article.getStatus()==2){
            article.setPublishTime(LocalDateTime.now());
            article.setUpdateTime(LocalDateTime.now());
        }else if(article.getStatus()==1){
            article.setSubmitTime(LocalDateTime.now());
            article.setUpdateTime(LocalDateTime.now());
        }else {
            article.setUpdateTime(LocalDateTime.now());
        }
        return articleMapper.insertArticle(article);
    }

    @Override
    public int updArticle(Article article) {
        article.setUpdateTime(LocalDateTime.now());//更新修改时间
        if(article.getStatus()==1) {
            article.setSubmitTime(LocalDateTime.now());
        }
        return articleMapper.updArticle(article);
    }

    @Override
    public PageResult getAuditArticlePage(Integer page, Integer pageSize, Integer articleId) {
        PageHelper.startPage(page,pageSize);
//        2.执行查询
        List<Article> articleAuditList = articleMapper.AuditList(articleId);
        Page<Article> p = (Page<Article>) articleAuditList;
//        3.封装PageResult对象
        PageResult pageResult = new PageResult(p.getResult(),p.getTotal(),p.getPageSize(),p.getPageNum());
        return pageResult;
    }

    @Override
    public int updAproveArticle(Article article) {
        article.setUpdateTime(LocalDateTime.now());//更新修改时间
        article.setPublishTime(LocalDateTime.now());//更新发布时间

        article.setStatus(2);
        return articleMapper.updAproveArticle(article);
    }

    @Override
    public int updRejectArticle(Article article) {
        article.setUpdateTime(LocalDateTime.now());//更新修改时间
        article.setStatus(3);
        return articleMapper.updRejectArticle(article);
    }

}
